<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>{{ i18n('product.from') }}</div>
        </div>

        <div class="admin_main_block_right">
          <div>
            <el-button icon="Back" @click="router.go(-1)">
              {{ i18n('common.return') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_form_main">
        <el-form label-width="120px" ref="formRef" :model="dataForm" :rules="dataRule">
          <el-form-item :label="i18n('wechat.product.product_id')" prop="product_id">
            <el-select v-model="dataForm.product_id" :placeholder="i18n('common.please_select') + i18n('wechat.product.product_id')" :remote-method="loadProductList" :loading="loading" reserve-keyword filterable remote clearable>
              <el-option v-for="(v, k) in productList" :label="v.title" :key="k" :value="v.id">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="dataFormSubmit(formRef)">
              {{ i18n('common.confirm') }}
            </el-button>
            <el-button @click="resetForm(formRef)">
              {{ i18n('common.reset') }}
            </el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script setup name="wechat-product-form">
  import { useBase } from "@/hooks/base/useBase"
  import { useForm } from "@/hooks/form/useForm"

  const { router, query, i18n, isAuth } = useBase()

  const model = ref('wechat/product')

  const loading = ref(false)
  const productList = ref([])

  const dataForm = reactive({
    product_id: '',
  })

  const dataRule = reactive({
    product_id: [{
      required: true,
      message: i18n('wechat.product.rules.product_id.require'),
      trigger: 'blur',
    }],
  })

  const {
    formRef,
    dataFormSubmit,
    resetForm
  } = useForm(model.value, dataForm)

  const loadProductList = (title) => {
    if (title) {
      loading.value = true
      $http({
        url: $http.adornUrl('/business/product/select'),
        method: 'get',
        params: $http.adornParams({
          title: title
        }),
      }).then(({ data }) => {
        if (data && data.status === 200) {
          productList.value = data.data
          loading.value = false
        } else {
          $message.error(data.message)
        }
      })
    } else {
      productList.value = []
    }
  }

</script>
